<?php
class StaffModel extends model
{
	static $_instance = null;

	static function getInstance ()
	{
		if (is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	// 员工(总件数)
	function getCount($p){
		$sqltemp  = "select aa.*,if(aa.sstop=1,'停用','启用') as stopname,ca.name as agency,cd.name as position ";
		$sqltemp .= "from (select cc.id as cid,su.id as sid,su.user as users,su.agency_id,su.position_id,su.stop as sstop,cc.* "; 
		$sqltemp .= "      from crm_staff as cc,sys_user as su ";
		$sqltemp .= "      where su.ugroup = 2 and cc.login_id = su.id ";
		$sqltemp .= "     )aa left join crm_agency as ca on ca.id = aa.agency_id ";
		$sqltemp .= "         left join crm_position as cd on cd.id = aa.position_id ";
		$sql  = "select count(*) as c from ($sqltemp)a where 1=1 ";
		if($p["user"] != ""){$sql .= " and users like '%".$p["user"]."%'";}
		if($p["name"] != ""){$sql .= " and name like '%".$p["name"]."%'";}
		if($p["agency"] != ""){$sql .= " and agency_id in (".$p["agency"].")";}

		$return = $this->sql($sql)->s(1);
		return $return["c"];
	}
	
	// 员工(列表)
	function getStaff($p)
	{
		$sqltemp  = "select aa.*,if(aa.sstop=1,'停用','启用') as stopname,ca.name as agency,cd.name as position ";
		$sqltemp .= "from (select cc.id as cid,su.id as sid,su.user as users,su.agency_id,su.position_id,su.stop as sstop,cc.* "; 
		$sqltemp .= "      from crm_staff as cc,sys_user as su ";
		$sqltemp .= "      where su.ugroup = 2 and cc.login_id = su.id ";
		$sqltemp .= "     )aa left join crm_agency as ca on ca.id = aa.agency_id ";
		$sqltemp .= "         left join crm_position as cd on cd.id = aa.position_id ";
		$sql = "select * from ($sqltemp)a where 1=1 ";
		if($p["user"] != ""){$sql .= " and users like '%".$p["user"]."%'";}
		if($p["name"] != ""){$sql .= " and name like '%".$p["name"]."%'";}
		if($p["agency"] != ""){$sql .= " and agency_id in (".$p["agency"].")";}
		$sql .= " order by ".$p["sortField"]." ".$p["sortOrder"];
		$sql .= " limit [start],[end]";

		return $this->sql($sql,array("start"=>$p["r"]*$p["p"],"end"=>$p["r"]))->s();
	}
	
	// 员工(列表)
	function getStaffList($p)
	{
		$sqltemp  = "select aa.*,ca.name as agency,cd.name as position ";
		$sqltemp .= "from (select cc.id as cid,su.id as sid,su.user as users,su.agency_id,su.position_id,cc.* "; 
		$sqltemp .= "      from crm_staff as cc,sys_user as su ";
		$sqltemp .= "      where su.stop=0 and su.ugroup = 2 and cc.login_id = su.id ";
		$sqltemp .= "     )aa left join crm_agency as ca on ca.id = aa.agency_id ";
		$sqltemp .= "         left join crm_position as cd on cd.id = aa.position_id ";
		$sqltemp .= "where aa.agency_id=[id] ";

		return $this->sql($sqltemp,$p)->s();
	}
	
	// 员工(详情)
	function getStaffInfo($p)
	{
		$sqltemp  = "select aa.*,ca.name as agency,cd.name as position ";
		$sqltemp .= "from (select cc.id as cid,su.id as sid,su.user as users,su.agency_id,su.position_id,cc.* "; 
		$sqltemp .= "      from crm_staff as cc,sys_user as su ";
		$sqltemp .= "      where su.ugroup = 2 and cc.login_id = su.id ";
		$sqltemp .= "     )aa left join crm_agency as ca on ca.id = aa.agency_id ";
		$sqltemp .= "         left join crm_position as cd on cd.id = aa.position_id ";
		$sqltemp .= "where aa.sid =[sid] ";

		return $this->sql($sqltemp,$p)->s(1);
	}
	
	// 员工状态修改
	function UserStatusUpd($p)
	{
		$this->beginTransaction();
		$rs = $this->sql("update crm_staff set stop=[stop] where id=[cid] ",$p)->d();
		if(@mysql_errno() != 0){
			$this->rollBack();
			return false;
		}
		
		$rs = $this->sql("update sys_user set stop=[stop] where id=[sid] ",$p)->d();
		if(@mysql_errno() != 0){
			$this->rollBack();
			return false;
		}
		
		$rs = $this->sql("update sys_user_role set stop=[stop] where `user`=[sid] ",$p)->d();
		$this->commit();
		return true;
	}

    /**
     * 删除员工
     *
     * @param  array $p
     * @return bool
     */
    function StaffDel($p)
    {
        $this->beginTransaction();
        $rs = $this->sql("delete from crm_staff where id=[cid] ",$p)->d();
        if(@mysql_errno() != 0){
            $this->rollBack();
            return false;
        }

        $rs = $this->sql("delete from sys_user where id=[sid] ",$p)->d();
        if(@mysql_errno() != 0){
            $this->rollBack();
            return false;
        }

        $rs = $this->sql("delete from sys_user_role  where `user`=[sid] ",$p)->d();
        $this->commit();
        return true;
    }
	
	// 增加员工表
	function UserAdd($p)
	{
		$this->beginTransaction();
		
		//增加用户
		$rs = FrameworkModel::getInstance()->setUsersAdd($p);
		if((@mysql_errno() == 0) && $rs>0){
			//增加员工
			$p['login_id'] = $rs;
			$rs = $this->sql("insert into crm_staff(`login_id`,`name`,`sex`,`mobile_phone`,`tel`,`email`,`remark`)values([login_id],'[name]','[sex]','[mobile_phone]','[tel]','[email]','[remark]')",$p)->i();
			if((@mysql_errno() == 0) && $rs>0){
				//增加角色
				$p['user'] = $p['login_id'];
				FrameworkModel::getInstance()->setUserRole($p);
				$this->commit();
				return true;
			}else{
				$this->rollBack();
				return false;
			}
		}else{
			$this->rollBack();
			return false;
		}
	}
	
	// 修改员工表
	function UserUpd($p)
	{
		//修改用户
		FrameworkModel::getInstance()->setUsersEdit($p);

		//修改员工
		$rs = $this->sql("update crm_staff set `sex`='[sex]',`mobile_phone`='[mobile_phone]',`tel`='[tel]',`email`='[email]',`remark`='[remark]' where id=[cid] ",$p)->i();

		//修改角色
		FrameworkModel::getInstance()->setUserRole($p);
		return true;
	}
	
	//职位
	function StaffPosition($p){
		return $this->sql("select * from crm_position where agency=[agency]",$p)->s();
	}
	
	function userExists($p){
		$rs=$this->sql("select count(id) as c from sys_user where user='[user]'",$p)->s(1);
		if($rs["c"]>0){
			return true;
		}else{
			return false;
		}
	}
	
	function staffExists($p){
		$rs=$this->sql("select count(id) as c from crm_staff where name='[name]'",$p)->s(1);
		if($rs["c"]>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 增加角色
	 * Enter description here ...
	 */
	function RoleAdd($p){
		$this->sql("replace into sys_role (`name`,`desc`)values('[name]','[desc]')",$p)->i();
	}
	
	/**
	 * 修改角色
	 * Enter description here ...
	 */
	function RoleUpd($p){
		$this->sql("update sys_role set `name`='[name]',`desc`='[desc]' where id=[id] ",$p)->u();
	}
	
	/**
	 * 删除角色
	 * Enter description here ...
	 */
	function RoleDel($p){
		$rs = $this->sql("select count(*) as c from sys_user_role where role=[id] ",$p)->s(1);
		if($rs["c"] > 0){
			return false;
		}
		$this->sql("delete from sys_role where id=[id] ",$p)->i();
		return true;
	}
	
	function RoleSave($p){
		$this->sql("update sys_role set `permissions`='[role]' where id=[id] ",$p)->u();
	}
	
	function getStaffByName($id){
		$re = $this->sql("select name from crm_staff where id=$id ")->s(1);
		if($re){
			return $re['name'];
		}else{
			return '';
		}
	}
	
	function getStaffByPhone($id){
		$re = $this->sql("select mobile_phone from crm_staff where id=$id ")->s(1);
		if($re){
			return $re['mobile_phone'];
		}else{
			return '';
		}
	}
}